package com.wxq.yaoxia.drug;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.wxq.net.NetClient;
import com.wxq.net.NetWorkThread;
import com.wxq.yaoxia.R;
import com.wxq.yaoxia.TestWebActivity;
import com.wxq.yaoxia.YXBaseNetActivity;
import com.wxq.yaoxia.mainborad.YXMainboradActivity;
import com.wxq.yaoxia.util.YXConstants;
import com.wxq.yaoxia.util.YXUtility;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class YXDrugDetailActivity extends YXBaseNetActivity {

	ListView introductionLV;
	View nameView;
	ImageView arrowIV ;
	String queryId;
	JSONArray dataArray;
	LinearLayout row1;
	LinearLayout row2;
	Button sameDrugBtn;
	
	Handler mHandler = new Handler();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentViewWithTopbar(R.layout.yx_drug_detail, R.layout.yx_topbar);
		
		row1 = (LinearLayout) findViewById(R.id.row1);
		row2 = (LinearLayout) findViewById(R.id.row2);
		sameDrugBtn = (Button) findViewById(R.id.sameDrugBtn);
		
		Intent intent= getIntent();
	
		String name = intent.getStringExtra(YXConstants.COLUMN_Name);
		String brandName = intent.getStringExtra(YXConstants.DRUG_COLUMN_BrandName);
		String commonName = intent.getStringExtra(YXConstants.DRUG_COLUMN_CommonName);
		queryId = intent.getStringExtra(YXConstants.COLUMN_QueryId);		
		
		nameView = findViewById(R.id.nameLayout);
		arrowIV = (ImageView) findViewById(R.id.arrowIV);
		if(!TextUtils.isEmpty(name))
		{
			TextView nameTV = (TextView) nameView.findViewById(R.id.mainNameTV);
			nameTV.setText(name);
		}
		if(!TextUtils.isEmpty(commonName))
		{
			TextView subNameTV = (TextView) nameView.findViewById(R.id.subNameTV);
			subNameTV.setText(commonName);
		}
		if(!commonName.equals(brandName))
		{
			arrowIV.setVisibility(View.VISIBLE);
			nameView.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// 查询
					Intent intent = new Intent();
					intent.setClass(YXDrugDetailActivity.this, YXDrugSearchResultActivity.class);
					intent.putExtra(YXConstants.YX_USE, YXConstants.YX_USE_DRUG);
					intent.putExtra(YXConstants.COLUMN_QueryId, queryId);
					
					YXDrugDetailActivity.this.startActivity(intent);
					//finish();
				}
			});
			
		}	
		else {
			arrowIV.setVisibility(View.INVISIBLE);
		}
		
		sameDrugBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 查询
				Intent intent = new Intent();
				intent.setClass(YXDrugDetailActivity.this, YXDrugSearchResultActivity.class);
				intent.putExtra(YXConstants.YX_USE, YXConstants.YX_USE_DRUG);
				intent.putExtra(YXConstants.COLUMN_QueryId, queryId);
				
				YXDrugDetailActivity.this.startActivity(intent);
			}
		});
		
		introductionLV = (ListView) findViewById(R.id.introductionLV);
		
		//获得网络数据
		NetWorkThread netWorkThread = new NetWorkThread(this, this, mHandler);
		netWorkThread.start();
	}

	@Override
	public void setTopBarButtonListener() {
		// TODO Auto-generated method stub
		Button leftBtn = (Button) findViewById(R.id.btnLeft);
    	Button rightBtnButton = (Button) findViewById(R.id.btnRight);
    	TextView centerTV = (TextView) findViewById(R.id.centerTV);
    	
    	leftBtn.setVisibility(View.VISIBLE);
    	leftBtn.setText("返回");
    	leftBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
    	rightBtnButton.setVisibility(View.VISIBLE);
//    	rightBtnButton.setText("药");
    	rightBtnButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(YXDrugDetailActivity.this, YXMainboradActivity.class);
				YXDrugDetailActivity.this.startActivity(intent);
			}
		});
    	centerTV.setVisibility(View.VISIBLE);
    	centerTV.setText(getIntent().getStringExtra(YXConstants.COLUMN_Name));
	}
	
	public void getNetworkData() {
		getIntroductionData();
	}
	public void afterNetwork() {
		//移除提示信息
		super.afterNetwork();
		//setIntroductionLV();
		addDurgButtons();
	}
	
	public void addDurgButtons() {
		if (dataArray == null) {
			return;
		}
		Display d = this.getWindowManager().getDefaultDisplay();
		int windowWidth = d.getWidth();
		int btnWidth = (windowWidth - 4*30)/3;
		for (int i = 0; i < dataArray.length(); i++) {
			ImageButton tempBtn = new ImageButton(this);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(btnWidth, btnWidth);
			lp.setMargins(0, 0, 30, 0);
			//tempBtn.setText(dataArray.optJSONObject(i).optString("infoName").toString());
			String titleStr = dataArray.optJSONObject(i).optString("infoName").toString();
			if ("警告".equals(titleStr)) {
				tempBtn.setImageResource(R.drawable.drug_btn_jg);
			}
			else if ("用法".equals(titleStr)) {
				tempBtn.setImageResource(R.drawable.drug_btn_yf);
			}
			else if("副作用".equals(titleStr)){
				tempBtn.setImageResource(R.drawable.drug_btn_fzy);
			}
			else if ("注意事项".equals(titleStr)) {
				tempBtn.setImageResource(R.drawable.drug_btn_zysx);
			}
			else if ("相互作用".equals(titleStr)) {
				tempBtn.setImageResource(R.drawable.drug_btn_xhzy);
			}
			else if ("小贴士".equals(titleStr)) {
				tempBtn.setImageResource(R.drawable.drug_btn_xts);
			}
			tempBtn.setPadding(0, 0, 0, 0);
			tempBtn.setScaleType(ScaleType.FIT_XY);
			tempBtn.setTag(new Integer(i));
			tempBtn.setOnClickListener(new View.OnClickListener() {	
				@Override
				public void onClick(View v) {
					Integer index = (Integer) v.getTag();
					Intent intent  = new Intent();
					intent.putExtra("url", dataArray.optJSONObject(index).optString("url"));
					intent.setClass(YXDrugDetailActivity.this, TestWebActivity.class);
					startActivity(intent);
				}
			});
			if (i < 3) {
				if (i == 2) {
					lp.setMargins(0, 0, 0, 0);
				}
				row1.addView(tempBtn, lp);
			}
			else {
				if (i == 5) {
					lp.setMargins(0, 0, 0, 0);
				}
				row2.addView(tempBtn, lp);
			}
		}
	}
	
	public void getIntroductionData() {
		String flag = "A90";
		String key = queryId;
		String timestamp = Calendar.getInstance().getTime().getTime()+""; 
		String checksum = YXUtility.encryptToMD5(timestamp+flag+timestamp+key+timestamp+"yaoxia");
		String url = String.format(YXConstants.URL_DETAIL, flag,key,timestamp,checksum.toLowerCase());
		//Log.d("wxq", "url = "+url);
		String result = NetClient.doGet(url);
		//Log.d("wxq", "result = "+result);
		if(TextUtils.isEmpty(result))
		{
			YXUtility.showToast(this, "Get data erro,please check your network!", mHandler);
			return ;
		}
		try {
			JSONObject resultJSON = new JSONObject(result);
			int code = resultJSON.getInt("code");
			String msg = resultJSON.getString("message");	
			if(code == 500)
			{
				YXUtility.showToast(this, msg, mHandler);
			}
			else if(code == 200)
			{
				dataArray = resultJSON.getJSONArray("drugInfo");
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public List<Map<String, String>> fillIntroductionData()
	{
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		if(dataArray == null)
		{
			return list;
		}
		for(int i=0;i<dataArray.length();i++)
		{
			try {
				Map<String, String>tempMap = new HashMap<String, String>();
				tempMap.put("name", dataArray.getJSONObject(i).getString("infoName"));
				list.add(tempMap);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return list;
	}
	
	
	
	public void setIntroductionLV() {
		List<Map<String, String>> list = fillIntroductionData();
		//ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.shool_item, list);
		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.shool_item, new String[]{"name"}, new int []{android.R.id.text1});
		introductionLV.setAdapter(simpleAdapter);
		introductionLV.setOnItemClickListener(
				new OnItemClickListener()
				{
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						Intent intent  = new Intent();
						intent.putExtra("url", dataArray.optJSONObject(position).optString("url"));
						intent.setClass(YXDrugDetailActivity.this, TestWebActivity.class);
						startActivity(intent);
					}				
				});
	}
	
}
